package com.hyfrogx.modules.app.controller.information;

/**
 * @author liubin
 * @date 2019/10/31 16:25
 */
/*
 * 获取所有分类
 *
 * @return
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.entity.HyUserAttentionEntity;
import com.hyfrogx.modules.app.service.AttentionService;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.information.entity.HyInformationVerifyEntity;
import com.hyfrogx.modules.information.service.HyInformationVerifyService;
import com.hyfrogx.modules.information.service.InfoCommentService;
import com.hyfrogx.modules.information.service.InformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "api/free/information")
@Api(value = "ApiFreeInformationController", tags = {"客户端--资讯模块免登陆"})
public class ApiFreeInformationController {


  @Autowired
  private HyInformationVerifyService informationVerifyService;
  @Autowired
  private AttentionService attentionService;
  @Autowired
  private InformationService informationService;
  @Autowired
  private InfoCommentService commentService;

  /**
   * 资讯首页（未关注）
   */
  @RequestMapping(value = "index", method = RequestMethod.GET)
  @ApiOperation("资讯首页（未关注）")
  public R getAllVerifyRecordClient(@RequestParam Map<String, Object> map) {
    map.put("limit", 9);
    // 查询所有未关注的资讯号
    Page page = informationVerifyService.getAllNotFollowClient(new QueryExt<>(map));
    List<HyInformationVerifyEntity> result = page.getRecords();
    // 查询所有推荐的资讯号
    List<HyInformationVerifyEntity> resultrecommend = informationVerifyService.getAllRecommendReordClient();
    if (resultrecommend != null) {

      for (HyInformationVerifyEntity record : resultrecommend) {
        if (map.containsKey("uid")) {
          Integer uid = Integer.valueOf(map.get("uid").toString());
          HyUserAttentionEntity hyUserAttention = new HyUserAttentionEntity();
          // 被关注者ID
          hyUserAttention.setSid(record.getUid());
          // 粉丝ID
          hyUserAttention.setFid(uid);
          HyUserAttentionEntity fun = attentionService.getByFid(uid);
          // 0表示无关系，1表示存在关系
          Integer status = 0;
          if (fun == null) {
            record.setIsfollow(status);
          } else {
            HyUserAttentionEntity funs = attentionService.getBaseMapper()
              .selectOne(new QueryWrapper<HyUserAttentionEntity>()
                .eq("sid", record.getUid()).eq("fid", uid));
            if (funs == null) {
              status = 0;
              record.setIsfollow(status);
            } else {
              status = 1;
              record.setIsfollow(status);
            }
          }
        }
      }
    }
    map = new HashMap<>();
    map.put("recommendResult", resultrecommend);
    map.put("notfollowResult", result);
    QueryExt query = new QueryExt(map);
    return R.ok().put("data", query);
  }

  /**
   * 根据分类获取分类下所有内容和下拉获取数据
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "RandomGetInformationByContentType", method = RequestMethod.GET)
  @ApiOperation("根据分类获取分类下所有内容和下拉获取数据")
  public R RandomGetInformationByContentType(@RequestParam Map<String, Object> map) {
    map.put("page",1);
    map.put("limit",10);
    map.put("contentType",1);
    map.put("sourceNum", 100001);
    if (map.containsKey("contentType")) {
          if (map.get("contentType").equals(FrogConstant.Information.CONTENT_TYPE_UNKNOW)) {
            map.remove("contentType");
            map.put("isRecommend", 1);
      }
    }
    return R.ok().put("data", informationService.RandomGetInformationByContentType(new QueryExt<>(map)));
  }

  /**
   * 获取所有推荐资讯
   *
   * @return
   */
  @RequestMapping(value = "information/getallRecommendInformation", method = RequestMethod.GET)
  @ApiOperation("获取所有推荐资讯")
  public R getAllRecommendInformation(@RequestParam Map<String, Object> map) {
    return R.ok().put("data", informationService.getallRecommendInformation(new QueryExt<>(map)));
  }

  /**
   * 营运中心
   * @param map
   * @return
   */
  @RequestMapping(value = "information/OperationsCenter", method = RequestMethod.GET)
  @ApiOperation("营运中心")
  public R getAllOperationsCenter(@RequestParam Map<String, Object> map) {
    try {
      Page page = informationVerifyService.getAllOperationCenter(new QueryExt<>(map));
      return R.ok().put("data", page);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return R.error("营运中心查询异常");
  }

  /**
   * 获取当前资讯全部评论
   *
   * @return
   */
  @RequestMapping(value = "information/getInformationComment", method = RequestMethod.GET)
  @ApiOperation("获取当前资讯全部评论")
  public R getInformationComment(@RequestParam Map<String, Object> map) {
    try {
      if(!map.containsKey("infoId")){
        return R.error("请选择资讯");
      }
      Page page = commentService.getInformationComment(new QueryExt<>(map));
      return R.ok().put("data", page);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

}
